package com.shopping.servlet;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.shopping.bean.User;
import com.shopping.service.UserService;
import com.shopping.service.impl.UserServiceImpl;

/**
 * @author zjh
 *
 */
@WebServlet("/ChangeUserMassage.do")
public class ChangeUserMassageServlet extends HttpServlet {
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		FileItemFactory fac = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(fac);
		upload.setHeaderEncoding("UTF-8");
		if(upload.isMultipartContent(req)) {
			String username = null;
			String password = null;
			String address = null;
			String phone = null;
			String userSex = null;
			String savePath = null;
			String fileName = null;
			
			try {
				List<FileItem> items = upload.parseRequest(req);
				for (FileItem item : items) {
					if(item.isFormField()) {
						if("username".equals(item.getFieldName())){
							username = item.getString("UTF-8");
						}
						if("password".equals(item.getFieldName())){
							password = item.getString("UTF-8");
						}
						if("address".equals(item.getFieldName())){
							address = item.getString("UTF-8");
						}
						if("phone".equals(item.getFieldName())){
							phone = item.getString("UTF-8");
						}
						if("userSex".equals(item.getFieldName())){
							userSex = item.getString("UTF-8");
						}
					}else {
						if(item.getSize() >1024 * 1024) {
							req.setAttribute("msg", "文件过大，请选择小于1m的图片上传");
							req.getRequestDispatcher("updateUserMassage.jsp").forward(req, resp);
							return;
						}
						
						String realPath = "/shopping/images/userImage";
						File folder = new File(realPath);
						if(!folder.exists()) {
							folder.mkdirs();
						}
						
						fileName = System.currentTimeMillis()+item.getName().substring(item.getName().lastIndexOf("."));
						File desFile = new File(realPath,fileName);
						System.out.println(desFile);
						try {
							item.write(desFile);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
				UserService us = new UserServiceImpl();
				savePath = "/shopping/images/userImage/"+fileName;
				int userId = us.getUserId(username, password);
				User user = new User(userId,username,password,address,phone,userSex);
				user.setUserImage(savePath);
				System.out.println(savePath);
				int num = us.updateUserMsg(user);
				if(num == 0) {
					resp.sendRedirect("updateUserMassage.jsp");
					return;
				}
				req.getSession().setAttribute("loginUser", user);
				req.getRequestDispatcher("login.jsp").forward(req, resp);
			} catch (Exception e) {
				resp.sendRedirect("updateUserMassage.jsp");
				e.printStackTrace();
			}
		}
	}
}
